<template>
	<div id="elementNew" class="elementNew">
		<group>
			<cell class="newLessonCell" title="类型" :value="type" is-link @click.native="showPopup=true"></cell>
			<x-input title="标题" v-model="value"></x-input>
			<!--<x-textarea title="类型说明" v-model="value_description" placeholder="在这里写点什么" :show-counter="false" :rows="1" autosize></x-textarea>-->
			<vue-editor v-model="value_description" :editorToolbar="customToolbar"></vue-editor>
			<!--<vue-wangeditor id="editor" v-model="value_description"></vue-wangeditor>-->
		</group>

		<div class="error" v-show="error">{{error}}</div>

		<div v-transfer-dom>
			<popup v-model="showPopup" class="checker-popup">
				<div style="padding:10px 10px 40px 10px;">
					<p style="padding: 5px 5px 5px 2px;color:#888;">角色类型</p>
					<checker
						v-model="type"
						default-item-class="checker-item"
						selected-item-class="checker-item-selected"
						disabled-item-class="checker-item-disabled">
						<checker-item v-for="(item,index) in items" :value="item.value" @on-item-click="onItemClick(item.value)"> {{item.value}} </checker-item>
					</checker>
				</div>
			</popup>
		</div>

		<flexbox>
			<flexbox-item>
				<x-button class="flexBtn_confirm" @click.native="confirm">确定</x-button>
			</flexbox-item>
			<flexbox-item>
				<x-button class="flexBtn_cancel" @click.native="cancel">取消</x-button>
			</flexbox-item>
		</flexbox>
	</div>
</template>

<script>
	import bus from '@/event/eventBus'
	import NetWorking from '../../../utils/networking'
	import {Group, XInput, XTextarea, XButton, Flexbox, FlexboxItem, TransferDom, Popup, Checker, CheckerItem, Cell} from 'vux'
	import { VueEditor } from 'vue2-editor'
	//import vueWangeditor from 'vue-wangeditor'

	const URL = 'list_of_value'
	const TYPE = 'S_LOV_LESSON'
	export default {
		name: "new-list-of-value",
		components: {
			Group,
			XInput,
			XTextarea,
			XButton,
			Flexbox,
			FlexboxItem,
			VueEditor,
			TransferDom,
			Popup,
			Checker,
			CheckerItem,
			Cell
		},
		directives: {
			TransferDom
		},
		data() {
			return {
				type: '',
				lov_type: '',
				showPopup: false,
				value: '',
				sequence: '',
				value_description: '',
				items: [{id: 1, value: 'introduction', description: '介绍'}, {id: 2, value: 'refuse', description: '抗拒点'}],
				title: this.$route.params.title,
				params: this.$route.params.params,
				error: '',
				customToolbar: [
					['bold', 'italic', 'underline'],
					[{ 'list': 'bullet' }],
					[{ 'indent': '-1'}, { 'indent': '+1' }],
					[{ 'color': [] }]
				]
			}
		},
		watch: {
			type: function () {
				switch (this.type){
					case 'introduction':
						this.lov_type = this.params + '_INTRODUCTION'
						break;
					case 'refuse':
						this.lov_type = this.params + '_REFUSE'
						break;
					default:
						break;
				}
			}
		},
		created: function () {
			bus.$emit("barController", 1)
			bus.$emit("setTopSimpleTitle", this.title)
		},
		methods: {
			onItemClick (value) {
				this.type = value
				this.showPopup = false
			},
			cancel: function () {
				this.$router.go(-1)
			},
			confirm: function () {

				if(this.value == ''){
					this.error = "请输入所有必填项"
					return
				}

				NetWorking.doPost(
					URL,
					null,
					{
						type: this.lov_type,
						value: this.value,
						value_description: this.value_description,
						sequence: this.sequence
					}
				).then(
					response => {
						let rs = response.data
						if(rs.error_code == 0){
							this.$router.go(-1)
						}else{
							alert(rs.error_message)
						}
					}
				)
			}
		}
	}
</script>

<style>
	.elementNew {
		border: solid;
		border-color: #f7f7fa;
		margin-left: 5px;
		margin-right: 5px;
		margin-top: 56px;
		box-shadow: 5px 5px 5px #888888;
		border-radius: 25px;
		background-color: white;
	}

	.buttonRole {
		width: 100%;
		margin-top: 15px;
		border-radius: 25px;
	}
</style>